// pages/shdigl/shdigl.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      data:[],
      userId:'',
      checked:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    //获取用户id 没有去登录  
    var userInfo = wx.getStorageSync("userInfo");
    if (userInfo){
      //console.log(userInfo);
      this.setData({
        userId: userInfo.id
      })
    }else{
      wx.showModal({
        title: '提示',
        content: '请先登录账号',
        success (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/user/user'
            });
          } else if (res.cancel) {
            wx.navigateBack({//返回
              delta: 1
            })
          }
        }
      })
    }
    //获取收货地址
    var userId = that.data.userId;
    console.log(userId);
    app.wxRequest('POST','/Home/User/address',{
      "userId":userId
    },function(res){
      console.log(res);
      if(res.data.code == 1){
        that.setData({
          data:res.data.data
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.onLoad();
    
  },

  addAddress: function() {
    wx.navigateTo({
      url: '/pages/address/address?status=add'
    });

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  choose(e){
    var that = this;
    var id = e.currentTarget.dataset.id;

    var userId = that.data.userId;
    app.wxRequest('POST','/Home/User/select_address',{
      "id":id,
      "user_id":userId
    },function(res){
      console.log(res);
      if(res.data.code == 1){
        that.setData({
          data:res.data.data
        })
      }
    })
    
    
  },
  del(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    var userId = that.data.userId;
    app.wxRequest('POST','/Home/User/del_address',{
      "id":id,
      'user_id':userId
    },function(res){
      console.log(res);
      if(res.data.code == 1){
        that.setData({
          data:res.data.data
        })
      }
    })

  }
})